package com.bwie.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.TbActiveRuleMapper;
import com.bwie.mapper.TbWinRecordMapper;
import com.bwie.pojo.TbActive;
import com.bwie.pojo.TbActiveRule;
import com.bwie.service.TbActiveService;
import com.bwie.mapper.TbActiveMapper;
import com.bwie.utils.R;
import com.bwie.utils.TokenUtil;
import com.bwie.vo.PageInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
* @author Lenovo
* @description 针对表【tb_active(抽奖活动表)】的数据库操作Service实现
* @createDate 2025-07-21 19:43:21
*/
@Service
public class TbActiveServiceImpl extends ServiceImpl<TbActiveMapper, TbActive>
    implements TbActiveService{
    @Autowired
    private TbActiveMapper tbActiveMapper;

    @Autowired
    private TbActiveRuleMapper tbActiveRuleMapper;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private TbWinRecordMapper tbWinRecordMapper;

    @Override
    public R getActiveListList(PageInfoVo pageInfoVo) {
        Page<TbActive> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        Page<TbActive> tbActivePage = tbActiveMapper.getActiveListList(page,pageInfoVo );
        return R.OK(tbActivePage);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R addactive(TbActive tbActive) {

        String token = request.getHeader("token");
        String userName = TokenUtil.getUserName(token);
        tbActive.setCreateBy(userName);
        tbActive.setCreateTime(new Date());
        tbActiveMapper.insert(tbActive);


        for (TbActiveRule tbActiveRule : tbActive.getActiveRuleList()) {
            tbActiveRule.setActiveId(tbActive.getActiveId());
            tbActiveRule.setCreateBy(userName);
            tbActiveRule.setCreateTime(new Date());
            tbActiveRuleMapper.insert(tbActiveRule);
        }
        return R.OK();
    }
}




